package com.thymeleaf.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thymeleaf.demo.lib.AdminLib;
import com.thymeleaf.demo.model.MallAdmin;
import com.thymeleaf.demo.mapper.MallAdminMapper;
import com.thymeleaf.demo.myexception.MyExcption;
import com.thymeleaf.demo.service.MallAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.thymeleaf.demo.util.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sunny
 * @since 2020-12-23
 */
@Service
public class MallAdminServiceImpl extends ServiceImpl<MallAdminMapper, MallAdmin> implements MallAdminService {

    @Autowired
    AdminLib adminLib;

    @Override
    public Boolean insertAdmin(MallAdmin admin) throws Exception {
        // 判断账号不重复
        QueryWrapper<MallAdmin> wrapper = new QueryWrapper<>();
        wrapper.eq("account", admin.getAccount());
        if(getOne(wrapper) != null){
            throw new MyExcption(ResultEnum.USERNAME_MORE.getCode());
        }

        // 设置密码盐加密的密码 md5(密码+密码盐)
        admin = adminLib.setPwd(admin.getPwd(), MallAdmin.class, admin);

        return save(admin);
    }
}
